package org.epes.laboratory.vcn.listener;

import java.io.ByteArrayInputStream;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.epes.laboratory.vcn.application.OptimizationApplication;

public class InformedDecisionCreateListener implements TaskListener {

	@Override
	public void notify(DelegateTask delegateTask) {			
		// Get the execution to have access to the process variables
		DelegateExecution execution = delegateTask.getExecution();
		// Get the solution
		OptimizationApplication oa = (OptimizationApplication) execution.getVariable("optimizationApplication");
		// Get excel solution byte array
		byte[] excelSolution = oa.getExcelSolution();	
		// Add the excelSolution as a task attachment
		Map<String, ProcessEngine> processEngines = ProcessEngines.getProcessEngines();
		ProcessEngine processEngine = processEngines.get("default");		
		TaskService taskService = processEngine.getTaskService();		
		taskService.createAttachment("xlsx", delegateTask.getId(), execution.getId(), "OptimizationSolution", 
				"Optimization solution for decision making", new ByteArrayInputStream(excelSolution));
	}
}
